<?php
//焦点图
//@author yuanjiang 932625974#qq.com
//@date 10.9.2016
if(!defined('IN_T')){
   die('hacking attempt');
}

$act = Common::sfilter($_REQUEST['act']);
// var_dump('her');
//详情: 查看或编辑或增加
if($act=='detail'){
   // var_dump('her');die;
   $sid = intval($_REQUEST['sid']);
   //传递了sid，验证sid是否存在，不存在强制赋值为0
   if($sid>0){
      $slide = $Db->query("select * from ".$Base->table('link')." where link_id=$sid","Row");
      if(!$slide){
         $sid = 0;
      }
   }
   //前台查看
   if(empty($_POST)){
      $tp->assign('slide',$slide);
   }
   //后台处理
   else{
      $data = array(
         'link_name' => Common::sfilter($_POST['link_name']),
         'link_link' => trim($_POST['link']),
         'link_sort' => intval($_POST['link_sort']),
      );
      $res['status'] = 0;
      if(empty($data['link_name'])){
         $res['msg'] = '请填写友情链接名称';
      }
      else if(!empty($data['link_link']) && !Common::is_url($data['link_link'])){
         $res['msg'] = '链接地址不正确';
      }
      else{
         if($sid>0){
            $data['link_id'] = $sid;
            }
        
         $Db->replace($Base->table('link'),$data);
         $res = array('status'=>1,'msg'=>'提交成功','href'=>'/'.ADMIN_PATH.'/?m=link');
      }
      echo $Json->encode($res);
      exit;
   }
   $tp->assign('act','detail');
}
else if($act=='delete'){
   //id
   $sid = intval($_REQUEST['sid']);
   $res['status'] = 0;
 
   $Db->delete($Base->table('link'),array('link_id'=>$sid));
   $res = array('status'=>1,'msg'=>'删除成功！');
   echo $Json->encode($res);
   exit;
}
//列表 
else{
   $list = $Db->query("select * from ".$Base->table('link')." order by link_sort asc, link_id desc");
   $tp->assign('list',$list);
   $tp->assign('act','list');
}

$tp->assign('nav','友情链接');
?>